﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using YT.DB;
using YT.Commons;
using YT.Modules;

namespace YT.XH.SuddenDeafness
{
    public partial class frmTherapeuticMethodDetail : Form
    {
        private string mstrPeriod = string.Empty;
        private string mstrSickRecID = string.Empty;
        public delegate void change(string strPeriod);
        public event change onchange;
        private void Test(string strPeriod)
        {
            string strSQL = string.Empty;
            UpdateData();

            if (onchange != null)
            {
                onchange(strPeriod);
            }
        } 

        public frmTherapeuticMethodDetail()
        {
            InitializeComponent();
        }

        public frmTherapeuticMethodDetail(string strPeriod,string strSickRecID)
        {
            InitializeComponent();
            mstrPeriod = strPeriod;
            mstrSickRecID = strSickRecID;
        }
        
        private void FillCase()
        {
            string strSQL = string.Empty;
            string strOperationName =string.Empty ;
            initlblDesc();
            strSQL = "select top 1 * from mTreatHistory where SickRecID='" + mstrSickRecID + "' and Period='" + mstrPeriod + "' order by recid desc";
            DBAccess dbcon =new DBAccess ( false,false );
            DataRow drRow = dbcon.GetDataFirstRow ( strSQL );
            if ( drRow !=null )
            {
                strOperationName = CvtFunc.ToString ( drRow ["TreatCaseName"]);
                GetRadFromOperationName(strOperationName );
            }
            
            if (rad方案A2.Checked)
            {                
                if (SetThrombolysis(this.pnl_方案A2, drRow))
                {
                    txt方案A2D.Text = CvtFunc.ToString(drRow["Thrombolysis"]);
                    rad方案A2C.Checked = true;
                }
            }
        }


        private bool SetThrombolysis(Panel pnl, DataRow drCur)
        {
            foreach (Control con in pnl.Controls)
            {
                if (con.GetType().Name == "RadioButton")
                {
                    if (CvtFunc.ToString(drCur["Thrombolysis"]).Trim() == con.Text.Trim())
                    {
                        ((RadioButton)con).Checked = true;
                        return false;
                    }
                }
            }
            return true;
        }

        private void initlblDesc()
        {
            DBAccess dbcon = new DBAccess(false, false);



            string strSQL = "select dateDiff(d,PainDate, FirstDate),CuredInOtherHosiptal,UseRS,UseKFJS from mSickPerson where SickRecID='" + mstrSickRecID + "'";
            DataRow drDays = dbcon.GetDataFirstRow(strSQL);
            int intDays = 0;
            bool blnCuredInOtherHis = false;   //已在外院接受过治疗
            bool blnUseRs = false;   // 治疗时已用过融栓药
            bool blnUseKFJS = false;  //是否在外院治疗用过口服激素？
            if (drDays != null)
            {
                intDays = CvtFunc.ToInt(drDays[0]);
                blnCuredInOtherHis = CvtFunc.ToBoolean(drDays["CuredInOtherHosiptal"]);
                blnUseRs = CvtFunc.ToBoolean(drDays["UseRS"]);
                blnUseKFJS = CvtFunc.ToBoolean(drDays["UseKFJS"]);
            }

            if (intDays > 30 || (blnCuredInOtherHis && blnUseKFJS))
            {
                lblDesc4.Text = "1. 第一阶段选用方案6 患者可继续沿用原治疗方案（方案6→方案A.1)";
                lblDesc3.Text = "2. 第一阶段选用方案7－9患者，停用降纤栓治疗。";                
            }

            string  strHospitalID = DataFunctions.GetFieldValue("mSickPerson","HospitalID"," sickrecid='"+  mstrSickRecID +"'"); 
            bool isInProvince = CvtFunc.ToBoolean (  DataFunctions.GetFieldValue ( "mHospital","InProvince","HospitalID='"+ strHospitalID +"'"));
            if (isInProvince)
            {
                lblDesc4.Text = "1. 第一阶段选用方案6 患者可继续沿用原治疗方案（方案6→方案A.1)";
                lblDesc3.Text = "2. 第一阶段选用方案7－10患者，停用降纤治疗。";        
            }
            dbcon.CloseCon();

            if (isInProvince)
            {
                rad方案A2C.Visible = false;
                txt方案A2D.Visible = false;
            }
        }

        private void btnSecondNext_Click(object sender, EventArgs e)
        {
            if (radSecondContinue.Checked)
                Test( mstrPeriod );
            else
            {
                DBAccess dbcon = new DBAccess(false, false);
                string strSQL = string.Empty;
                strSQL = "update mSickPerson set Finished=1 where SickRecID='" + mstrSickRecID + "'";
                dbcon.ExecuteSql(strSQL);
                dbcon.CloseCon();   
            }
        }

        public  bool UpdateData()
        {
            string strSQL = string.Empty;
            string strTreatCaseName =string.Empty ;
            string strThrombolysis =string.Empty ;

            strTreatCaseName = GetOperationNameFromRad();           
            if (rad方案A2.Checked)
            {
                foreach (Control con in this.pnl_方案A2.Controls)
                {
                    if (con.GetType().Name == "RadioButton")
                    {
                        if (((RadioButton)con).Checked)
                        {
                            strThrombolysis = con.Text;
                        }
                    }
                    if (CvtFunc.ToString(strThrombolysis) == "其它") strThrombolysis = txt方案A2D.Text; 
                }
            }
            strSQL = " if not exists ( select * from mTreatHistory where SickRecID='" + mstrSickRecID + "' and Period='" + mstrPeriod + "' and TreatCaseName='"+ strTreatCaseName +"')"
                    + " INSERT INTO mTreatHistory ( SickRecID, Period, TreatCaseName,Thrombolysis ) "
                    + " VALUES ( '" + mstrSickRecID + "', '" + mstrPeriod + "', '" + strTreatCaseName + "','" + strThrombolysis + "'  ) "
                    + " ELSE " 
                    + " UPDATE mTreatHistory Set TreatCaseName='"+ strTreatCaseName  +"' where SickRecID='"+ mstrSickRecID  +"' and Period='"+ mstrPeriod  +"' ";
            DBAccess dbcon = new DBAccess(false, false);
            dbcon.ExecuteSql(strSQL);
            return true;
        }
        private string GetOperationNameFromRad()
        {           
            if (rad方案A1.Checked) return "方案A.1";
            if (rad方案A2.Checked) return "方案A.2";            
           
            return "";
        }

        private void GetRadFromOperationName(string strOperationName)
        {
            
            if (strOperationName == "方案A.1") rad方案A1.Checked = true;
            if (strOperationName == "方案A.2") rad方案A2.Checked = true;
        }

        private void frmTherapeuticMethodDetail_Load(object sender, EventArgs e)
        {
            FillCase();
        }


    }
}
